package com.servlet;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.core.ApplicationContext;

import javafx.application.Application;

@WebServlet("/SessionDemo")
public class SessionDemo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public SessionDemo() {

    }
    
    @Override
    public void init() throws ServletException {
    	// TODO Auto-generated method stub
    	System.out.println("初始化...");
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String name=request.getParameter("name");
		String psw=request.getParameter("password");

		//session
		HttpSession session=request.getSession(true);
		session.setAttribute("name", name);	//存入seesion里面用set
		session.setAttribute("password", psw);
		
		System.out.println(session.getId());
		
		//application:作用范围是正个项目
//	    ServletContext application=request.getServletContext();
//		application.setAttribute("name", name);
//		application.setAttribute("password", psw);
		System.out.println(session.getAttribute("password"));
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("SessionDemo 被销毁了...");
	}
}
